package com.yantong.service.impl;

import com.yantong.constant.BalanceLogFlowType;
import com.yantong.constant.BalanceLogStyle;
import com.yantong.pojo.BalanceLog;
import com.yantong.service.BalanceLogService;
import com.yantong.pojo.Order;
import com.yantong.pojo.TradeConfig;
import com.yantong.service.ConfigService;
import com.yantong.pojo.OrderSpecialDTO;
import com.yantong.service.OrderSpecialService;
import com.yantong.pojo.Member;
import com.yantong.service.MemberIntegralService;
import com.yantong.service.MemberService;
import com.yantong.utils.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 订单特殊需求处理
 *
 * @author liaozhaogui-773104632@qq.com
 * @date 2021/2/1 16:36
 */
@Service
public class OrderSpecialServiceImpl implements OrderSpecialService {
	private Logger logger = LoggerFactory.getLogger(OrderSpecialService.class);

	@Autowired
	private ConfigService configService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private MemberIntegralService memberIntegralService;
	@Autowired
	private BalanceLogService balanceLogService;

	@Override
	public void orderBusinessProcess(OrderSpecialDTO orderSpecialDTO) {
		Order order = orderSpecialDTO.getOrder();
		Long centreId = order.getCentreId();
		Member member = memberService.getByKey(order.getMid());
		if (centreId == 359L) {
			//是春播商户时需要进行特殊需求业务处理
			this.chunBoBusinessProcess(order, member);
		} else {
			//通用处理
			//赠送积分
			this.presentOrder(order, member);
		}
	}

	/**
	 * 春播下单特殊需求处理
	 * （用户线上下订单一次系统赠送50积分。）
	 *
	 * @param order  订单信息
	 * @param member 用户信息
	 * @author liaozhaogui
	 * @date 2021/2/1 14:19
	 */
	private void chunBoBusinessProcess(Order order, Member member) {
		//获取积分配置
		TradeConfig tradeConfig = configService.getTradeConfig(member.getCentreId());
		if (tradeConfig != null && ValidateUtil.isNotEmptyString(tradeConfig.getPlaceTheOrder())) {
			//赠送积分   春播的积分使用的是余额的字段
			BigDecimal credit2Old = member.getCredit2();
			member.setCredit2(new BigDecimal(tradeConfig.getPlaceTheOrder()));
			memberService.updateMemberAsync(member);

			BalanceLog supplyBalanceLog = new BalanceLog();
			supplyBalanceLog.setMid(member.getId());
			supplyBalanceLog.setMobile(member.getMobile());
			supplyBalanceLog.setRealname(member.getRealname());
			supplyBalanceLog.setStatus(0);
			supplyBalanceLog.setType(1);
			supplyBalanceLog.setStyle(BalanceLogStyle.REGISTERED.getCode());
			supplyBalanceLog.setCredittype("credit2");
			supplyBalanceLog.setBeforeamount(credit2Old);
			supplyBalanceLog.setMoney(new BigDecimal(tradeConfig.getPlaceTheOrder()));
			supplyBalanceLog.setAfteramount(credit2Old.add(new BigDecimal(tradeConfig.getPlaceTheOrder())));
			supplyBalanceLog.setCreateTime(new Date());
			supplyBalanceLog.setCentreId(member.getCentreId());
			supplyBalanceLog.setTitle("新用户注册");
			supplyBalanceLog.setRemark("新用户注册赠送");
			supplyBalanceLog.setSource(0);
			supplyBalanceLog.setFlowtype(BalanceLogFlowType.REGISTERED.getCode());
			balanceLogService.save(supplyBalanceLog);
		}
	}

	/**
	 * 用户注册通用积分赠送处理
	 *
	 * @param order  订单信息
	 * @param member 用户信息
	 * @author liaozhaogui
	 * @date 2021/2/1 16:07
	 */
	private void presentOrder(Order order, Member member) {
		logger.info("-------------------进入注册积分逻辑---------");
		//获取积分配置
		TradeConfig tradeConfig = configService.getTradeConfig(member.getCentreId());
		if (tradeConfig != null && ValidateUtil.isNotEmptyString(tradeConfig.getPlaceTheOrder())) {
			//增加用户积分
			memberIntegralService.addMemberIntegral(member.getId(), new BigDecimal(tradeConfig.getPlaceTheOrder()), order.getId(), "用户下单赠送积分");
		}
	}
}
